package com.onLineCar.driver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.driver.domain.entity.*;
import com.onLineCar.driver.domain.entity.Request.ReqWallet;
import com.onLineCar.driver.domain.entity.Request.RequestDriver;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 司机管理表 Mapper 接口
 * </p>
 *
 * @author dsr
 * @since 2023-12-12
 */
@Mapper
public interface TDriverMapper extends BaseMapper<TDriver> {

//    @Select("select* from t_driver WHERE t_driver.check_state = 0")
//    List<TDriver> selectDriver(Integer checkState);

    @Select("select*  from t_driver " +
            "left join china on t_driver.city_id =china.Id " +
            "where driver_id =#{driverId}")
    TDriver queryDriverListByDriverId(Integer driverId);

    @Update("update t_driver set check_state=#{checkState} where driver_id=#{driverId}")
    int updateDriverStateById(RequestDriver requestDriver);

    @Update("UPDATE `driver`.`t_preempt` " +
            "SET " +
            "`user_id` = #{userId}, " +
            "`preempt_state` = #{preemptState}, " +
            "`preempt_version` = #{preemptVersion} " +
            "WHERE " +
            "`order_number` = #{orderNumber};")
    int updatePreempt(Preempt preempt);

   // @Select("select* from t_preempt where preempt_id=#{preemptId}


    @Select("select* from t_preempt where order_number=#{preemptNumber}")
    Preempt findByPreemptNumber(String preemptNumber);


    @Insert("INSERT INTO `driver`.`t_preempt` " +
            "( `order_number`, `driver_tell`, `user_id`, `preempt_state`, `preempt_version`) " +
            "VALUES " +
            "( #{orderNumber}, #{driverTell}, #{userId}, #{preemptState}, #{preemptVersion});")
    int insertPreempt(Preempt preempt);

    @Update("update t_driver set driver_state=3 where driver_tell=#{driverTell}")
    int updateDriverState(String driverTell);

    @Insert("INSERT INTO `driver`.`t_valuation` " +
            "( `valuation_begin_price`, `valuation_km_price`, `valuation_time_price`, `valuation_toll_price`, `valuation_night`, `valuation_special`, `valuation_car_type`, `valuation_sum_price`, `order_number`, `create_time`, `update_time`, `delete`) " +
            "VALUES " +
            "( #{valuationBeginPrice}, #{valuationKmPrice},#{valuationTimePrice}, #{valuationTollPrice}, #{valuationNight}, #{valuationSpecial}, #{valuationCarType}, #{valuationSumPrice}, #{orderNumber} , now(), now(), 0)")
    void insertValuation(Valuation valuation);

    @Select("select * from t_driver where car_id =#{carId}")
    TDriver selectDriverByCarId(List<String> carId);

    @Select("select * from t_wallet where t_wallet.user_id=#{walletId}")
    List<Wallet> selectWalletInfo(Integer walletId);

    @Select("select t_wellet_discount.wallet_id ,t_discount.discount_info " +
            "from t_wellet_discount " +
            "left join t_discount on t_wellet_discount.discount_id = t_discount.discount_id " +
            "where wallet_id = #{walletId}")
    List<Discount> selectDiscountInfo(Integer walletId);

    @Select("select * from t_valuation where order_number = #{orderNumber}")
    Valuation selectValuationByOrderNumber(String orderNumber);

    @Insert("INSERT INTO `driver`.`t_wallet` " +
            "(`wallet_Id`, `wallet_balance`, `discount_id`, `wallet_state`, `wallet_pwd`, `user_id`, `create_time`, `update_time`, `delete`) " +
            "VALUES " +
            "(null, 0.0, 0, 0, '000000', #{userId}, now(), now(), 0)")
    void insertWallet(Wallet wallet);

    @Update("update t_wallet set wallet_balance = wallet_balance + #{walletPrice} where user_id= #{userId}")
    void insertWalletPrice(ReqWallet reqWallet);

    @Select("SELECT * FROM t_wallet WHERE user_id = #{userId} AND wallet_pwd = #{pwd}")
    Wallet selectWallet(@Param("userId") Integer userId, @Param("pwd") String pwd);

    @Update("update t_wallet set wallet_balance = wallet_balance - #{walletPrice} where user_id= #{userId}")
    void subtractWalletPrice(@Param("userId") Integer userId, @Param("walletPrice") BigDecimal walletPrice);



//    class TDriverMapper implements RowMapper<Order> {
//        @Override
//        public Order mapRow(ResultSet rs, int rowNum) throws SQLException {
//            Order order = new Order();
//            order.setId(rs.getInt("id"));
//            order.setUserId(rs.getInt("user_id"));
//            order.setDriverId(rs.getInt("driver_id"));
//            order.setStatus(rs.getInt("status"));
//            order.setVersion(rs.getInt("version"));
//            return order;
//        }

}
